package it.polimi.traveldream.ejb;

import it.polimi.traveldream.dto.CartPackageDTO;
import it.polimi.traveldream.dto.GiftListElementDTO;
import it.polimi.traveldream.dto.PaidDTO;
import it.polimi.traveldream.entity.CartPackage;
import it.polimi.traveldream.entity.Customer;
import it.polimi.traveldream.entity.GiftlistElement;
import it.polimi.traveldream.entity.Paid;
import it.polimi.traveldream.entity.PaidPK;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class CustomerManagerBean implements CustomerManager {

	@PersistenceContext
	private EntityManager em;

	@Resource
	private EJBContext context;

	@Override
	public void addToCart(CartPackageDTO pack) {
		CartPackage newCart = new CartPackage(pack);
		Customer user = this.find(this.getEmail());
		user.addCartPackage(newCart);
		this.em.merge(user);
	}

	public Customer find(String email) {
		Customer user = this.em.createNamedQuery(Customer.FIND_USER, Customer.class)
				.setParameter("email", email).getSingleResult();
		return user;
	}

	public String getEmail() {
		return this.context.getCallerPrincipal().getName();
	}

	@Override
	public void addToGiftlist(CartPackageDTO pack) {
		CartPackage newCart = new CartPackage(pack);
		GiftlistElement newGift = new GiftlistElement();
		newGift.setCartpackage(newCart);
		Customer user = this.find(this.getEmail());
		user.addGiftlistElement(newGift);
		this.em.merge(user);
	}

	@Override
	public List<CartPackageDTO> getCart() {
		List<CartPackageDTO> list = new ArrayList<>();
		List<CartPackage> packages = this.em
				.createNamedQuery(CartPackage.FIND_ALL, CartPackage.class)
				.setParameter("user", this.find(this.getEmail())).getResultList();
		for (CartPackage pack : packages) {
			list.add(pack.convertToDTO());
		}
		return list;
	}

	@Override
	public void remove(CartPackageDTO pack) {
		this.em.remove(this.em.find(CartPackage.class, pack.getId()));
	}

	@Override
	public void purchase(CartPackageDTO pack) {
		CartPackage p = this.em.find(CartPackage.class, pack.getId());
		p.setPurchased(true);
		this.em.merge(p);
	}

	@Override
	public void doGift(PaidDTO paid) {
		PaidPK key = new PaidPK();
		key.setCartpackage(paid.getCartpackageId());
		key.setProduct(paid.getProduct().getId());
		key.setUser(paid.getUserId());
		Paid p = this.em.find(Paid.class, key);
		p.setQuantity(paid.getQuantity());
		this.em.merge(p);
	}

	@Override
	public List<GiftListElementDTO> getGiftList(Integer currentId) {
		List<GiftListElementDTO> list = new ArrayList<>();
		List<GiftlistElement> elements = this.em
				.createNamedQuery(GiftlistElement.FIND_ALL, GiftlistElement.class)
				.setParameter("user", this.em.find(Customer.class, currentId)).getResultList();
		for (GiftlistElement elem : elements) {
			list.add(elem.convertToDTO());
		}
		return list;
	}

	@Override
	public Integer getCurrentId() {
		return this.find(this.getEmail()).getId();
	}
}
